# 请在此输入您的代码
n, m = map(int, input().split())
inf = float('inf')
edges = [[inf for _ in range(n + 1)] for _ in range(n + 1)]
for _ in range(m):
    u, v, w = map(int, input().split())
    edges[u][v] = w
min_step = inf
for k in range(1, n + 1):
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            if edges[i][k] == inf or edges[k][j] == inf: continue
            edges[i][j] = min(edges[i][j], edges[i][k] ^ edges[k][j])
            if edges[i][j]:
                min_step = min(min_step, edges[i][j])
if min_step == inf:
    print(-1)
else:
    print(min_step)
